#!/bin/bash

plain='\033[0m'
info='\033[32m'
error='\033[41;30m'
success='\033[42;30m'

system_optimization(){
# 系统优化（禁用防火墙、selinux、swap，启用br_netfilter内核模块）
echo -e "${info} [3&4:1/3] System optimization... ${plain}"
systemctl stop firewalld && systemctl disable firewalld &>/dev/null
setenforce 0
sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
swapoff -a
sed -i '/centos-swap/s/^/#/' /etc/fstab
modprobe br_netfilter
echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables
}

install_docker(){
# 安装和配置docker
echo -e "${info} [3&4:2/3] Install dokcer-ce... ${plain}"
yum install -y yum-utils device-mapper-persistent-data lvm2  &>/dev/null
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo  &>/dev/null
yum install -y docker-ce-20.10.15  &>/dev/null
mkdir -p /etc/docker
cat <<EOF >/etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "registry-mirrors": ["https://ustc-edu-cn.mirror.aliyuncs.com"],
  "storage-driver": "overlay2",
  "storage-opts": ["overlay2.override_kernel_check=true"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m",
    "max-file": "3"
  }
}
EOF
systemctl daemon-reload  &>/dev/null
systemctl restart docker &>/dev/null
systemctl enable docker  &>/dev/null
}

install_kubernetes(){
# 安装配置k8s
echo -e "${info} [3&4:3/3] Install kubernetes... ${plain}"
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubelet-1.23.0 kubeadm-1.23.0 kubectl-1.23.0  &>/dev/null
systemctl enable kubelet &>/dev/null
systemctl start kubelet  &>/dev/null
}

system_optimization 
install_docker
install_kubernetes